package com.xjtu.chapter07.listener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author yjq
 * @version 1.0
 * @date 2021/8/21 15:45
 */
public class EventDemo1 {
    Frame frame = new Frame("测试事件处理");

    TextField textField = new TextField(30);
    //事件源
    Button ok = new Button("确定");

    public void init(){
        //组装视图
        //监听器
//        MyListener myListener = new MyListener();
        //注册监听
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("ok按钮点击");
                textField.setText("hello world");
            }
        });//用匿名类比较方便
        frame.add(textField,BorderLayout.NORTH);
        frame.add(ok);

        frame.pack();
        frame.setVisible(true);

    }
//    public class MyListener implements ActionListener{
//        @Override
//        public void actionPerformed(ActionEvent actionEvent) {
//            textField.setText("hello world");
//        }
//    }//内部类的写法

    public static void main(String[] args) {
        new EventDemo1().init();
    }
}
